/*****************************************************************************/
/**
*
* @file key.c
*
* User defined functions about gpios of stm32.
* Target device: STM32F103RET6.
*
* <pre>
* MODIFICATION HISTORY:
*
* Ver   Who    Date   	Changes
* ----- ---- ---------- -------------------------------------------------------
* 0.01  abu  01/29/2020 Created
* </pre>
*
*------------------------------------------------------------------------------
* Author:	abu
*					E-mail:	abu_liu@opencores.org
*------------------------------------------------------------------------------
*
******************************************************************************/

/***************************** Include Files *********************************/
#include "bsp_key.h"
#include "bsp_misc.h"

/************************** Function Prototypes ******************************/

/*****************************************************************************/
/**
*
* @brief	Configure all keys. All keys will be configured as general inputs,
*					without interrupt.
*
* @param	None.
* @param  None.	
*
* @return	None.
*
* @note		None.
*
******************************************************************************/
void key_init(void)
{
	GPIO_InitTypeDef key;
	
	//KEY2
	key.GPIO_Pin = KEY2_PIN;
	key.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(KEY2_PORT, &key);
	
	//KEY3
	key.GPIO_Pin = KEY3_PIN;
	key.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(KEY3_PORT, &key);

	//KEY2
	key.GPIO_Pin = KEY4_PIN;
	key.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(KEY4_PORT, &key);

	//KEY2
	key.GPIO_Pin = KEY5_PIN;
	key.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(KEY5_PORT, &key);

}

/*****************************************************************************/
/**
*
* Scan the specified key.
*
*
* @param	None.
* @param    None.	
*
* @return	None.
*
* @note		None.
*
******************************************************************************/
uint8_t KeyScan(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
    if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_ON)
    {
        //while(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_ON);
        return KEY_ON;
    }
    else
        return KEY_OFF;
}

/*****************************************************************************/
/**
*
* KEY2 IRQ handler.
*
*
* @param	None.
* @param  None.	
*
* @return	None.
*
* @note		None.
*
******************************************************************************/
void KEY2_IRQHandler(void)
{
	
}

/*****************************************************************************/
/**
*
* KEY3 IRQ handler.
*
*
* @param	None.
* @param  None.	
*
* @return	None.
*
* @note		None.
*
******************************************************************************/
void KEY3_IRQHandler(void)
{
	
}

/*****************************************************************************/
/**
*
* KEY4 IRQ handler.
*
*
* @param	None.
* @param  None.	
*
* @return	None.
*
* @note		None.
*
******************************************************************************/
void KEY4_IRQHandler(void)
{
	if(EXTI_GetITStatus(KEY4_INT_EXTI_LINE) != RESET)
	{
		Led_Yellow_Toggle();
		EXTI_ClearITPendingBit(KEY4_INT_EXTI_LINE);
	}
}

/*****************************************************************************/
/**
*
* KEY5 IRQ handler.
*
*
* @param	None.
* @param  None.	
*
* @return	None.
*
* @note		None.
*
******************************************************************************/
void KEY5_IRQHandler(void)
{
	
}

/*****************************************************************************/
/**
*
* @brief	set key4 as interrupt input.
*
*
* @param	None.
* @param  None.	
*
* @return	None.
*
* @note		None.
*
******************************************************************************/
void Key4_Intr_Init(void)
{
	GPIO_InitTypeDef Key4_GPIO;
	EXTI_InitTypeDef Key4_EXTI;
	
	//initialise GPIO
	Key4_GPIO.GPIO_Pin = KEY4_PIN;
	Key4_GPIO.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(KEY4_PORT, &Key4_GPIO);

	//initialise EXTI
	GPIO_EXTILineConfig(KEY4_INT_EXTI_PORTSOURCE, KEY4_INT_EXTI_PINSOURCE);
	Key4_EXTI.EXTI_Line = KEY4_INT_EXTI_LINE;
	Key4_EXTI.EXTI_Mode = EXTI_Mode_Interrupt;
	Key4_EXTI.EXTI_Trigger = EXTI_Trigger_Rising;

	Key4_EXTI.EXTI_LineCmd = ENABLE;
	EXTI_Init(&Key4_EXTI);
}
